﻿/**
//  OpenSessionCommand.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/
using System;
using System.Runtime.InteropServices;
using EDSDKLib;
using MarqueeMoment.CameraController.Camera;
using MarqueeMoment.Forms;
using MarqueeMoment.Entities;
using NLog;

namespace MarqueeMoment.CameraController.Command
{
    public class OpenSessionCommand : Command
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public OpenSessionCommand(CameraModel model)
            : base(model)
        {
        }

        //// Execute a command.	
        public override bool execute()
        {
            uint err = EDSDK.EDS_ERR_OK;
            bool locked = false;

            frmTakePhoto.UpdateSpinner(true);
            //// Open session with remote camera.            
            err = EDSDK.EdsOpenSession(model.getCameraObject());
            frmTakePhoto.UpdateSpinner(false);
            if (err != EDSDK.EDS_ERR_OK)
            {
                logger.Error("Can't open the session with the camera Error Code- " + err.ToString() + " With hex value- " + String.Format("{0:x2}", err));
                Dialogs.Error("Can't open the session with the camera because it is being used by some other application.\nClose that application and try again.\n(error code: " + err.ToString() + " with hex value: " + String.Format("{0:x2}", err) + ")");
            }
            else
            {
                frmTakePhoto.ShowLiveView();
            }

            if (base.model.isLegacy())
            {
                //Preservation ahead is set to PC

                if (err == EDSDK.EDS_ERR_OK)
                {
                    uint saveTo = (uint)EDSDK.EdsSaveTo.Host;

                    err = EDSDK.EdsSetPropertyData(base.model.getCameraObject(), EDSDK.PropID_SaveTo, 0, Marshal.SizeOf(saveTo), saveTo);

                }

                GetPropertyCommand getPropertyCommand = default(GetPropertyCommand);
                getPropertyCommand = new GetPropertyCommand(base.model, EDSDK.PropID_Unknown);
                getPropertyCommand.execute();

                GetPropertyDescCommand getPropertyDescCommand = default(GetPropertyDescCommand);
                getPropertyDescCommand = new GetPropertyDescCommand(base.model, EDSDK.PropID_Unknown);
                getPropertyDescCommand.execute();


            }
            else
            {
                //Preservation ahead is set to PC

                if (err == EDSDK.EDS_ERR_OK)
                {
                    uint saveTo = (uint)EDSDK.EdsSaveTo.Host;
                    err = EDSDK.EdsSetPropertyData(base.model.getCameraObject(), EDSDK.PropID_SaveTo, 0, Marshal.SizeOf(saveTo), saveTo);

                }


                //UI lock

                if (err == EDSDK.EDS_ERR_OK)
                {
                    err = EDSDK.EdsSendStatusCommand(base.model.getCameraObject(), EDSDK.CameraState_UILock, 0);
                }


                if (err == EDSDK.EDS_ERR_OK)
                {
                    locked = true;

                }



                if (err == EDSDK.EDS_ERR_OK)
                {
                    EDSDK.EdsCapacity capacity = default(EDSDK.EdsCapacity);
                    capacity.NumberOfFreeClusters = 0x7fffffff;
                    capacity.BytesPerSector = 0x1000;
                    capacity.Reset = 1;

                    err = EDSDK.EdsSetCapacity(base.model.getCameraObject(), capacity);

                }

                //It releases it when locked

                if (locked)
                {
                    EDSDK.EdsSendStatusCommand(base.model.getCameraObject(), EDSDK.CameraState_UIUnLock, 0);

                }

            }


            //Notification of error

            if (err < EDSDK.EDS_ERR_OK)
            {
                //CameraEvent e("error", &err);
                base.model.notifyObservers(commandName.errr, err);

            }


            return true;
        }

    }
}
